(function($){
	$.validator.defaults.ignore = '';

	/**
	 * tabs 扩展方法
	 */
	$.extend($.fn.tabs.methods, {
	    getTabById: function(jq,id) {
	        var tabs = $.data(jq[0], 'tabs').tabs;
	        for(var i=0; i<tabs.length; i++){
	            var tab = tabs[i];
	            if (tab.panel('options').id == id){
	                return tab;
	            }
	        }
	        return null;
	    },
	    selectById:function(jq,id) {
	        return jq.each(function() {
	            var state = $.data(this, 'tabs');
	            var opts = state.options;
	            var tabs = state.tabs;
	            var selectHis = state.selectHis;
	            if (tabs.length == 0) {return;}
	            var panel = $(this).tabs('getTabById',id); // get the panel to be activated 
	            if (!panel){return}
	            var selected = $(this).tabs('getSelected');
	            if (selected){
	                if (panel[0] == selected[0]){return}
	                $(this).tabs('unselect',$(this).tabs('getTabIndex',selected));
	                if (!selected.panel('options').closed){return}
	            }
	            panel.panel('open');
	            var title = panel.panel('options').title;        // the panel title 
	            selectHis.push(title);        // push select history 
	            var tab = panel.panel('options').tab;        // get the tab object 
	            tab.addClass('tabs-selected');
	            // scroll the tab to center position if required. 
	            var wrap = $(this).find('>div.tabs-header>div.tabs-wrap');
	            var left = tab.position().left;
	            var right = left + tab.outerWidth();
	            if (left < 0 || right > wrap.width()){
	                var deltaX = left - (wrap.width()-tab.width()) / 2;
	                $(this).tabs('scrollBy', deltaX);
	            } else {
	                $(this).tabs('scrollBy', 0);
	            }
	            $(this).tabs('resize');
	            opts.onSelect.call(this, title, $(this).tabs('getTabIndex',panel));
	        });
	    },
	    existsById:function(jq,id){
	        return $(jq[0]).tabs('getTabById',id) != null;
	    },
	    closeById: function(jq,id) {
	        var tab = $(jq[0]).tabs('getTabById',id);
	        if(tab!=null){
	        	var index = tab.panel('options').index;
	        	$(jq[0]).tabs('close',index)
	        }
	    },

	});

	$.extend($.fn.datagrid.methods,{
		"getDefaultWidth":function(jq, option){
			var $datagrid = jq.parents(".datagrid")
			var $datagridContent = jq.closest(".datagrid");
//			var datagridOffset = $datagrid.width() - $datagridContent.width();
			var datagridLeft = $datagridContent.offset().left;
			var datagridWidth = $(window).width() - (datagridLeft*2);// - datagridOffset;
			return datagridWidth;
//			return jq.closest(".wrapper-content").width()-($(".wrapper-content").attr("padding")*2);
		},
		"getDefaultHeight":function(jq, option){
			var windowHeight = $(window).height();
			var $datagridContent = jq.closest(".datagrid");
			var datagridTop = $datagridContent.offset().top;
			var tableHeigth = windowHeight - datagridTop;// - datagridoffset;
			return tableHeigth;
		},
		"getCheckedIds":function(jq, option){
			var ids = [];
			var rowArr = jq.datagrid("getChecked");
			for(var i in rowArr){
				ids.push(rowArr[i].id);
			}
			return ids;
		}
	});
	
	
	$.extend($.fn.datagrid.defaults,{
		"rownumbers" : true,
		"singleSelect":false,
		"selectOnCheck":false,
		"checkOnSelect":true,
		"striped":true,
		"checkbox":true,
		"pagination" : true,
		"pageSize":50,
		"pageList":[50,100,200],
		"onLoadError":function(arguments){
			$.messagebox.toast("加载数据异常"+arguments.responseText,"error");
		},
		"onLoadSuccess":function(){
		}
	});
	$.extend($.fn.pagination.defaults,{
		layout : [ 'list', 'sep', 'first', 'prev', 'sep', 'links',
					'sep', 'next', 'last', 'sep', 'refresh', 'sep',
					'manual' ],
		beforePageText : '转到',
		afterPageText : '页',
		displayMsg : '当前显示 {from} - {to} 条记录   共 {total} 条记录'
	});
	
	
	
	
	
	$.extend($.fn.treegrid.methods,{
		"getDefaultWidth":function(jq, option){
			var $treegrid = jq.parents(".datagrid")
			var $treegridContent = jq.parent();
//			var treegridOffset = $treegrid.width() - $treegridContent.width();
			var treegridLeft = $treegridContent.offset().left;
			var treegridWidth = $(window).width() - (treegridLeft*2);// - treegridOffset;
			return treegridWidth;
//			return jq.closest(".wrapper-content").width()-($(".wrapper-content").attr("padding")*2);
		},
		"getDefaultHeight":function(jq, option){
			var windowHeight = $(window).height();
			var $treegridContent = jq.parent();
			var treegridTop = $treegridContent.offset().top;
			var tableHeigth = windowHeight - treegridTop;// - treegridoffset;
			return tableHeigth;
		}
	});
	
	$.extend($.fn.treegrid.defaults,{
		"idField" : "id",
		"treeField" : "name",
		"rownumbers" : true,
		"singleSelect":true,
		"selectOnCheck":false,
		"checkOnSelect":true,
		"striped":true,
//		"checkbox":true,
		"pagination" : true,
//		"pageSize":50,
//		"pageList":[50,100,200],
//		"width":$(this).treegrid("getDefaultWidth"),
//		"heigth":$(this).treegrid("getDefaultHeight")
//		"onLoadSuccess":function(){
////			$(this).datagrid("initCheckbox");
//		}
		"onLoadError":function(arguments){
			$.messagebox.toast("加载数据异常"+arguments.responseText,"error");
		},
		"onBeforeLoad":function(){
			$(this).treegrid('getPager').pagination({
		        layout:['sep','refresh','sep'],
		        displayMsg:"树形数据"
		    })
		},
		//这是为了解决 singleSelect 导致无法取消选中
		"onBeforeSelect":function(node){
			var nodeSelected = $(this).treegrid("getSelected");
			if(nodeSelected!=null && nodeSelected.id == node.id){
				$(this).treegrid("unselect",node.id);
				return false;
			}
		}
	});	
	
	
	
	
	
	
	
	
	
	
	
})(jQuery);


